//
//  WorkorderCell.m
//  CarLife
//
//  Created by yu on 2018/2/1.
//  Copyright © 2018年 jpas. All rights reserved.
//

#import "WorkorderCell.h"
#import "WorkorderModel.h"

@implementation WorkorderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    UIView *contentView = self.contentView;
    CGFloat margin0 = 0.0;
    CGFloat margin10 = 10.0;
    
    _workorderLabel1.sd_layout
    .leftSpaceToView(contentView, margin10)
    .topSpaceToView(contentView, margin10)
    .rightSpaceToView(contentView, margin10)
    .heightIs(22);
    
    _workorderLabel2.sd_layout
    .leftEqualToView(_workorderLabel1)
    .topSpaceToView(_workorderLabel1, margin10)
    .rightEqualToView(_workorderLabel1)
    .heightIs(22);
    
    _workorderLabel3.sd_layout
    .leftEqualToView(_workorderLabel2)
    .topSpaceToView(_workorderLabel2, margin10)
    .rightEqualToView(_workorderLabel2)
    .heightIs(22);
    
    _lineView.sd_layout
    .leftSpaceToView(contentView, margin0)
    .rightSpaceToView(contentView, margin0)
    .topSpaceToView(_workorderLabel3, margin10)
    .heightIs(1);
    
    _receiveBtn.sd_layout
    .leftSpaceToView(contentView, margin10)
    .rightSpaceToView(contentView, margin10)
    .topSpaceToView(_lineView, margin0)
    .heightIs(43);
    
    _enterBtn.sd_layout
    .leftSpaceToView(contentView, margin10)
    .rightSpaceToView(contentView, margin10)
    .topSpaceToView(_lineView, margin0)
    .heightIs(43);
}

- (void)setWorkorderModel:(WorkorderModel *)workorderModel
{
    _workorderModel = workorderModel;
    
    _workorderLabel1.text = NSStringFormat(@"工单类型：%@",[self workorderType]);
    _workorderLabel2.text = NSStringFormat(@"工单编号：%@",workorderModel.workOrderNo);
    NSString *status = workorderModel.status;
    NSString *wordOrderType = workorderModel.wordOrderType;
    if ([wordOrderType isEqualToString:@"4"]) {
        _workorderLabel3.text = NSStringFormat(@"家访类型：%@",[self homeVisType]);
        [_enterBtn setTitle:@"进入家访" forState:UIControlStateNormal];
    } else {
        _workorderLabel3.text = NSStringFormat(@"车架号：%@",workorderModel.vin);
        [_enterBtn setTitle:@"进入拍照" forState:UIControlStateNormal];
    }
    
    UIView *bottomView;
    CGFloat margin;
    
    if ([status isEqualToString:@"9"]) {//已接工单
        _receiveBtn.hidden = YES;
        _enterBtn.hidden = NO;
        _lineView.hidden = NO;
        bottomView = _enterBtn;
        margin = 0.0;
    } else if ([status isEqualToString:@"2"]) {//未接工单
        _receiveBtn.hidden = NO;
        _enterBtn.hidden = YES;
        _lineView.hidden = NO;
        bottomView = _receiveBtn;
        margin = 0.0;
    } else {//已完成工单
        _receiveBtn.hidden = YES;
        _enterBtn.hidden = YES;
        _lineView.hidden = YES;
        bottomView = _workorderLabel3;
        margin = 10.0;
    }
    
    [self setupAutoHeightWithBottomView:bottomView bottomMargin:margin];
}

- (NSString *)workorderType {
    NSString *wordOrderType = NSStringFormat(@"%@",self.workorderModel.wordOrderType);
    if ([wordOrderType isEqualToString:@"0"]) {
        wordOrderType = @"新装";
    } else if ([wordOrderType isEqualToString:@"1"]) {
        wordOrderType = @"维护";
    } else if ([wordOrderType isEqualToString:@"2"]) {
        wordOrderType = @"拆机";
    } else if ([wordOrderType isEqualToString:@"3"]) {
        wordOrderType = @"移机";
    } else if ([wordOrderType isEqualToString:@"4"]) {
        wordOrderType = @"家访";
    }
    return wordOrderType;
}

- (NSString *)homeVisType {
    NSString *homeVisitsType = self.workorderModel.homeVisitsType;
    if ([homeVisitsType isEqualToString:@"1"]) {
        homeVisitsType = @"家庭拜访";
    } else if ([homeVisitsType isEqualToString:@"2"]) {
        homeVisitsType = @"员工公司拜访";
    } else {
        homeVisitsType = @"私营业主公司拜访";
    }
    return homeVisitsType;
}

- (IBAction)receiveAction:(UIButton *)btn
{
    if (_receiveWorkorderBlock) {
        _receiveWorkorderBlock(btn.tag - 1);
    }
}
- (IBAction)enterAction:(UIButton *)btn
{
    if (_enterWorkorderBlock) {
        _enterWorkorderBlock(btn.tag - 1);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
